package com.shop2cn.iapi.sqrefund.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ymttest.business.service.shenqi.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.resp.Order;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqrefund.parameter.PartAmountRefundReq;
import com.shop2cn.iapi.sqrefund.service.PartAmountRefundCall;
import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.model.sqrefund.RefundBill;
import com.ymttest.database.model.sqrefund.RefundBillIndex;
import com.ymttest.database.model.sqrefund.RefundProductDetail;
import com.ymttest.database.sharding.db.ext.model.OrderDetailExtPo;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sharding.model.PaymentPo;
import com.ymttest.database.sharding.db.sharding.model.UserRefundFundDetailPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sharding.db.sqlwapper.OrderWapper;
import com.ymttest.database.sqlwapper.sqrefund.RefundBillIndexWrapper;
import com.ymttest.database.sqlwapper.sqrefund.RefundBillWrapper;
import com.ymttest.database.sqlwapper.sqrefund.RefundProductDetailWrapper;
import com.ymttest.utils.DataUtil;

/**
 * 发货 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_PartAmountRefund {

	private static PartAmountRefundReq partAmountRefundReq;
	private static PartAmountRefundCall partAmountRefundCall;

	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	private static OrderWapper orderWapper = new OrderWapper();

	private static RefundBillWrapper refundBillWrapper = new RefundBillWrapper();
	private static RefundProductDetailWrapper refundProductDetailWrapper = new RefundProductDetailWrapper();
	private static RefundBillIndexWrapper refundBillIndexWrapper = new RefundBillIndexWrapper();

	private static long autotest_goodsOwnerId = ShenqiUserSet.autotest_goodsOwnerId;
	private static long chainStoreGoodsOwnerId = ShenqiUserSet.chainStoreGoodsOwnerId;
	private static long cdfMembergoodsOwnerId = ShenqiUserSet.cdfMembergoodsOwnerId;
	private static long virtualCoinGoodsOwnerId = ShenqiUserSet.virtualCoinGoodsOwnerId;
	private static long multiCDFCodeGoodsOwnerId = ShenqiUserSet.multiCDFCodeGoodsOwnerId;
	private static long macaoCDFGoodsOwnerId = ShenqiUserSet.macaoCDFGoodsOwnerId;
	private static long auditRefundGoodsOwnerId = ShenqiUserSet.auditRefundGoodsOwnerId;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("仅退款");
	}

	@Before
	public void caseUp() {

		partAmountRefundReq = new PartAmountRefundReq();
		partAmountRefundCall = new PartAmountRefundCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(partAmountRefundCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public BigDecimal[] apportionRefundAmount(
			List<OrderDetailPo> orderDetailPoList, BigDecimal totalAmount,
			List<String> refundedBillNos) {

		BigDecimal[] weights = new BigDecimal[orderDetailPoList.size()];

		int i = 0;

		for (OrderDetailPo orderDetailPo : orderDetailPoList) {

			String catalogId = orderDetailPo.getCatalogId();
			BigDecimal refundedAmount = BigDecimal.ZERO;
			int refundedCount = 0;
			
			if (null == refundedBillNos)
				refundedBillNos = Arrays.asList("");

			for (String refundedBillNo : refundedBillNos) {
				
				if (refundedBillNo.equalsIgnoreCase(""))
					break;

				RefundBill refundBill = refundBillWrapper
						.selectByPrimaryKey(refundedBillNo);

				// 退单被拒绝
				if (refundBill.getMch_review_status() == 3)
					continue;

				RefundProductDetail refundProductDetail = refundProductDetailWrapper
						.selectByRefundBillNoAndCatalogId(refundedBillNo,
								catalogId)
						.stream()
						.filter(detail -> detail.getCatalog_id()
								.equalsIgnoreCase(catalogId)).findFirst()
						.orElse(null);

				if (refundProductDetail != null)
					refundedAmount = refundedAmount.add(refundProductDetail
							.getRefund_product_amount());
			}

			BigDecimal productAmount = Utils
					.zeroIfNull(orderDetailPo.getProductPrice())
					.multiply(new BigDecimal(orderDetailPo.getProductCount()))
					.add(Utils.zeroIfNull(orderDetailPo.getDiscount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo.getYmtCouponAmount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo
									.getSellerCouponAmount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo.getPromotionAmount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo
									.getYmtPromotionAmount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo
									.getIntegralDeductionAmount()));

			weights[i] = productAmount.subtract(refundedAmount);

			i = i + 1;
		}

		BigDecimal[] result = Utils.apportionByWeightsAndMaxLimit(totalAmount,
				weights, weights, null);

		return result;
	}

	public void verify(long orderId) {
		this.verify(orderId, null, false);
	}

	public void verify(long orderId, List<String> refundedBillNos) {
		this.verify(orderId, refundedBillNos, false);
	}

	public void verify(long orderId, List<String> refundedBillNos,
			Boolean isDelivery) {

		OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);

		List<OrderDetailPo> orderDetailPoList = orderShardingWapper
				.selectOrderDetailbyOrderId(orderId);

		orderDetailPoList = orderDetailPoList
				.stream()
				.sorted(Comparator.comparing(OrderDetailPo::getCatalogId)
						.reversed()).collect(Collectors.toList());

		BigDecimal[] refundAmounts = this.apportionRefundAmount(
				orderDetailPoList, partAmountRefundReq.getApplyAmount(),
				refundedBillNos);

		int i = 0;
		String refundNo = null;

		// 整单退款，每个商品一个退款单
		for (OrderDetailPo orderDetailPo : orderDetailPoList) {

			BigDecimal refundFreightAmount = BigDecimal.ZERO;
			BigDecimal refundTaxAmount = BigDecimal.ZERO;

			BigDecimal refundProductAmount = refundAmounts[i];

			// 创建expectRefundProductDetail
			RefundProductDetail expectRefundProductDetail = new RefundProductDetail();

			OrderDetailExtPo orderDetailExtPo = orderWapper
					.selectOrderDetailExtByOrderId(orderDetailPo
							.getOrderDetailId());

			expectRefundProductDetail.setOrder_id(orderId);
			expectRefundProductDetail.setCatalog_id(orderDetailPo
					.getCatalogId());
			expectRefundProductDetail.setProduct_num(orderDetailPo
					.getProductCount());
			expectRefundProductDetail.setProduct_title(orderDetailExtPo
					.getTitle());
			expectRefundProductDetail
					.setRefund_product_amount(refundProductAmount);
			expectRefundProductDetail
					.setRefund_freight_amount(refundFreightAmount);
			expectRefundProductDetail.setRefund_tax_amount(refundTaxAmount);
			expectRefundProductDetail.setRefund_num(0);
			
			// 仅退款没有积分
			expectRefundProductDetail.setRefund_integral(0L);

			refundNo = this.verifyRefundProductDetailTable(
					expectRefundProductDetail, refundedBillNos);

			i = i + 1;
		}

		// 仅退款，多个商品，只会生成一个退款单
		RefundBill expectRefundBill = new RefundBill();

		Long userId = orderPo.getUserId();
		Long applyUserId = 0L;
		String submitTradeResult = null;
		Integer refundStatus = 0;

		// 是否买家提交
		Boolean isUserApply = false;

		if (userId.intValue() == partAmountRefundReq.getUserId().intValue()
				&& userId.longValue() != orderPo.getGoodsOwnerId())
			isUserApply = true;

		if (isUserApply)
			applyUserId = userId;
		else {

			refundStatus = 3;
			applyUserId = partAmountRefundReq.getCsId() == null ? partAmountRefundReq
					.getUserId() : partAmountRefundReq.getCsId();
		}

		BigDecimal totalRefundProductAmount = Arrays.asList(refundAmounts)
				.stream().reduce(BigDecimal.ZERO, BigDecimal::add);

		expectRefundBill.setOrder_id(orderId);
		expectRefundBill.setRefund_type(1);
		expectRefundBill.setPlace_order_time(orderPo.getAddTime());
		expectRefundBill
				.setTotal_refund_product_amount(totalRefundProductAmount);
		expectRefundBill.setTotal_refund_tax_amount(BigDecimal.ZERO);
		expectRefundBill.setTotal_refund_freight_amount(BigDecimal.ZERO);
		
		expectRefundBill.setOrder_status(isDelivery ? 3 : orderPo
				.getOrderStatus());
		expectRefundBill.setSeller_id(orderPo.getSellerId());
		expectRefundBill.setGoods_owner_id(orderPo.getGoodsOwnerId());
		expectRefundBill.setUser_id(userId);
		expectRefundBill.setApply_user_id(applyUserId);
		expectRefundBill.setRefund_status(refundStatus);
		expectRefundBill.setCurrent_review_mch_id(orderPo.getGoodsOwnerId());
		expectRefundBill.setMch_review_status(isUserApply ? 0 : 2);
		expectRefundBill.setSubmit_trade_status(1);
		expectRefundBill.setRemark(partAmountRefundReq.getRemark());
		expectRefundBill.setRefund_no(refundNo);
		expectRefundBill.setSubmit_trade_result(submitTradeResult);
		
		expectRefundBill.setTotal_refund_integral(0L);

		this.verifyRefundBillTable(orderId, expectRefundBill);
		
		RefundBillIndex expectResult = new RefundBillIndex();
		
		expectResult.setRefund_no(expectRefundBill.getRefund_no());
		expectResult.setMerchant_id(expectRefundBill.getSeller_id());
		expectResult.setOrder_status(expectRefundBill.getOrder_status());
		expectResult.setRefund_status(expectRefundBill.getRefund_status());
		expectResult.setOrder_id(orderId);
		expectResult.setRefund_type(expectRefundBill.getRefund_type());
		expectResult.setCurrent_review_mch_id(expectRefundBill.getCurrent_review_mch_id());
		expectResult.setMch_review_status(expectRefundBill.getMch_review_status());
		
		this.verifyRefundBillIndexTable(expectResult);
		
		List<RefundBill> refundBills = refundBillWrapper.selectByOrderId(orderId);
		
		BigDecimal totalProductAmount = refundBills.stream()
				.map(RefundBill::getTotal_refund_product_amount)
				.reduce(BigDecimal.ZERO, BigDecimal::add);
		BigDecimal totalTaxAmount = refundBills.stream()
				.map(RefundBill::getTotal_refund_tax_amount)
				.reduce(BigDecimal.ZERO, BigDecimal::add);
		BigDecimal totalFreightAmount = refundBills.stream()
				.map(RefundBill::getTotal_refund_freight_amount)
				.reduce(BigDecimal.ZERO, BigDecimal::add);
		
		BigDecimal totalRefundAmount = totalProductAmount.add(totalTaxAmount).add(totalFreightAmount);
		
		if (totalRefundAmount.compareTo(orderPo.getPayableAmount()) == 0)
			Logger.verifyEquals(true,
					SqTradingCallService.isCanceledOrder(orderPo),
					"验证订单是否取消");
		else
			Logger.verifyEquals(false,
					SqTradingCallService.isCanceledOrder(orderPo),
					"验证订单是否取消");
		
		if (refundStatus == 3) {
			
			boolean isAccountPaid = false;
			
			if (orderPo.getAccountPaidAmount().compareTo(BigDecimal.ZERO) == 1)
				isAccountPaid = true;
			else
				isAccountPaid = false;
			
			RefundService.buildUserRefundFundDetail(expectRefundBill,
					orderPo.getPaymentId(), isAccountPaid, BigDecimal.ZERO);
		}
	}

	private String verifyRefundBillTable(Long orderId, RefundBill expectResult) {

		String refundNo = expectResult.getRefund_no();

		Logger.comment(String.format("验证refund_bill表， orderId：%s，refundNo：%s",
				orderId, refundNo));

		RefundBill actualResult = refundBillWrapper.selectByRefundNoAndOrderId(
				orderId, refundNo);

		Logger.verifyEquals(expectResult.getRefund_type(),
				actualResult.getRefund_type(), "校验Refund_type");
		Logger.verifyEquals(refundNo, actualResult.getRefund_no(),
				"校验Refund_no");
		// DataUtil.verifyDateEquals(expectResult.getPlace_order_time(),
		// actualResult.getPlace_order_time(), "校验Place_order_time");
		DataUtil.verifyDecimalEquals(
				expectResult.getTotal_refund_product_amount(),
				actualResult.getTotal_refund_product_amount(),
				"校验Total_refund_product_amount");
		DataUtil.verifyDecimalEquals(expectResult.getTotal_refund_tax_amount(),
				actualResult.getTotal_refund_tax_amount(),
				"校验Total_refund_tax_amount");
		DataUtil.verifyDecimalEquals(
				expectResult.getTotal_refund_freight_amount(),
				actualResult.getTotal_refund_freight_amount(),
				"校验Total_refund_freight_amount");
		Logger.verifyEquals(expectResult.getOrder_status(),
				actualResult.getOrder_status(), "校验Order_status");
		Logger.verifyEquals(expectResult.getSeller_id(),
				actualResult.getSeller_id(), "校验Seller_id");
		Logger.verifyEquals(expectResult.getGoods_owner_id(),
				actualResult.getGoods_owner_id(), "校验Goods_owner_id");
		Logger.verifyEquals(expectResult.getUser_id(),
				actualResult.getUser_id(), "校验User_id");
		// DataUtil.verifyDateEquals(expectResult.getApply_time(),
		// actualResult.getApply_time(), "校验Apply_time");
		Logger.verifyEquals(expectResult.getApply_user_id(),
				actualResult.getApply_user_id(), "校验Apply_user_id");
		Logger.verifyEquals(expectResult.getRefund_status(),
				actualResult.getRefund_status(), "校验Refund_status");
		Logger.verifyEquals(expectResult.getCurrent_review_mch_id(),
				actualResult.getCurrent_review_mch_id(),
				"校验Current_review_mch_id");
		Logger.verifyEquals(expectResult.getMch_review_status(),
				actualResult.getMch_review_status(), "校验Mch_review_status");
		Logger.verifyEquals(expectResult.getSubmit_trade_status(),
				actualResult.getSubmit_trade_status(), "校验Submit_trade_status");
		Logger.verifyEquals(expectResult.getRemark(), actualResult.getRemark(),
				"校验Remark");
		
		Logger.verifyEquals(expectResult.getTotal_refund_integral(), actualResult.getTotal_refund_integral(),
				"校验Total_refund_integral");
		
		Logger.verifyEquals(partAmountRefundReq.getLiabilityType(),
				actualResult.getLiability_type(), "校验Liability_type");
		Logger.verifyEquals(partAmountRefundReq.getRefundExtType(),
				actualResult.getRefund_ext_type(), "校验Refund_ext_type");
		Logger.verifyEquals(partAmountRefundReq.getRefundReasonCode(),
				actualResult.getRefund_reason_code(), "校验Refund_reason_code");
		Logger.verifyEquals(partAmountRefundReq.getHasRefundGoods(),
				actualResult.getHas_refund_goods(), "校验Has_refund_goods");

		return actualResult.getRefund_no();
	}

	private String verifyRefundProductDetailTable(
			RefundProductDetail expectResult, List<String> refundedBillNos) {

		Long orderId = expectResult.getOrder_id();
		String catalogId = expectResult.getCatalog_id();

		Logger.comment(String.format(
				"验证refund_product_detail表， orderId：%s，catalogId：%s", orderId,
				catalogId));

		RefundProductDetail actualResult = null;
		
		if (null == refundedBillNos)
			refundedBillNos = Arrays.asList("");

		for (String refundedBillNo : refundedBillNos) {

			// 找到本次退款成功的退款单
			actualResult = refundProductDetailWrapper
					.selectLatestByOrderIdAndCatalogId(orderId, catalogId)
					.stream()
					.filter(refundProductDetail -> !refundProductDetail
							.getRefund_no().equalsIgnoreCase(refundedBillNo))
					.collect(Collectors.toList()).get(0);
		}

		DataUtil.verifyDecimalEquals(expectResult.getRefund_product_amount(),
				actualResult.getRefund_product_amount(),
				"校验Refund_product_amount");
		DataUtil.verifyDecimalEquals(expectResult.getRefund_tax_amount(),
				actualResult.getRefund_tax_amount(), "校验Refund_tax_amount");
		DataUtil.verifyDecimalEquals(expectResult.getRefund_freight_amount(),
				actualResult.getRefund_freight_amount(),
				"校验Refund_freight_amount");
		Logger.verifyEquals(expectResult.getRefund_num(),
				actualResult.getRefund_num(), "校验Refund_num");
		Logger.verifyEquals(expectResult.getProduct_num(),
				actualResult.getProduct_num(), "校验Product_num");
		Logger.verifyEquals(expectResult.getProduct_title(),
				actualResult.getProduct_title(), "校验Product_title");
		
		Logger.verifyEquals(expectResult.getRefund_integral(),
				actualResult.getRefund_integral(), "校验Refund_integral");

		return actualResult.getRefund_no();
	}

	private void verifyRefundBillIndexTable(RefundBillIndex expectResult) {
		
		Long merchantId = expectResult.getMerchant_id();
		
		Logger.comment(String.format(
				"验证refund_bill_index表， refundNo：%s，merchantId：%s", expectResult.getRefund_no(),
				merchantId));

		// 找到退款成规的退款详情单
		RefundBillIndex actualResult = refundBillIndexWrapper
				.selectByRefundNoAndMerchantId(expectResult.getRefund_no(),
						merchantId);

		Logger.verifyEquals(expectResult.getOrder_status(),
				actualResult.getOrder_status(),
				"校验Order_status");
		Logger.verifyEquals(expectResult.getRefund_status(),
				actualResult.getRefund_status(), "校验Refund_status");
		Logger.verifyEquals(expectResult.getOrder_id(),
				actualResult.getOrder_id(),
				"校验Order_id");
		Logger.verifyEquals(expectResult.getRefund_type(),
				actualResult.getRefund_type(), "校验Refund_type");
		Logger.verifyEquals(expectResult.getCurrent_review_mch_id(),
				actualResult.getCurrent_review_mch_id(), "校验Current_review_mch_id");
		Logger.verifyEquals(expectResult.getMch_review_status(),
				actualResult.getMch_review_status(), "校验Mch_review_status");
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_001() {
		Logger.start(false, "直销订单，货头申请仅退款成功");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_002() {
		Logger.start(false, "直销订单，未发货，买家申请仅退款，申请失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) auditRefundGoodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, auditRefundGoodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(userId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			Logger.verifyEquals("仅渠道商可发起部分退款", partAmountRefundCall.getMsg(),
					"验证resultMessage");
			Logger.verifyEquals("3", partAmountRefundCall.getCode(),
					"验证resultCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_003() {
		Logger.start(false, "代理订单，未发货，货头申请仅退款，退款失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);
			
			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			Logger.verifyEquals("代理订单不可部分退款", partAmountRefundCall.getMsg(),
					"验证resultMessage");
			Logger.verifyEquals("3", partAmountRefundCall.getCode(),
					"验证resultCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_004() {
		Logger.start(false, "直销订单，已发货，货头申请仅退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 发货
			SqTradingCallService.delivery(sellerId, orderId, null, null);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(4);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_005() {
		Logger.start(false, "直销订单多个商品，未发货，货头申请仅退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProsInOneOrder(productBean);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(new BigDecimal("0.01"));
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_006() {
		Logger.start(false, "直销订单，仅退款已成功的订单，再次仅退款，退款成功");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);
			
			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, new BigDecimal(
							"15.00"));

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_007() {
		Logger.start(false, "直销订单，订单正在仅退款，再次仅退款，退款失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 再次申请整单退款
			partAmountRefundCall.callService();

			Logger.verifyEquals("存在未完结的退款单", partAmountRefundCall.getMsg(),
					"验证resultMessage");
			Logger.verifyEquals("3", partAmountRefundCall.getCode(),
					"验证resultCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_009() {
		Logger.start(false, "直销订单，已经部分退款成功的订单，再次仅退款，退款成功");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			// 创建一个部分退款成功的退款单
			String refundedBillNo = RefundService
					.createPartProductRefundSuccess(orderId, orderDetailPo,
							autotest_goodsOwnerId, paymentId, userId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			this.verify(orderId, Arrays.asList(refundedBillNo));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_010() {
		Logger.start(false, "直销订单，盛付通支付，未发货，货头申请仅退款，退款成功");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer", "6010");

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6010);

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();
			
			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_011() {
		Logger.start(false, "直销订单，超过90天的订单，未发货，货头申请仅退款，退款失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			// 修改支付时间为90天前
			OrderPo record = new OrderPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DAY_OF_MONTH, -101);

			record.setOrderId(orderId);
			record.setPaidTime(calendar.getTime());

			orderShardingWapper.orderMapper.updateByPrimaryKeySelective(record);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			Logger.verifyEquals("订单已支付超过90天无法发起退款",
					partAmountRefundCall.getMsg(), "验证resultMessage");
			Logger.verifyEquals("3", partAmountRefundCall.getCode(),
					"验证resultCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_012() {
		Logger.start(false, "直销订单，包含虚拟库存，未发货，货头申请仅退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId, 2);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_013() {
		Logger.start(false, "直销订单，人民币标价商品，商户不允许收取人民币，未发货，货头申请仅退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) 500956988, 0, false, false);
			String currency = productBean.getCurrency();

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, null, 1, 500956988L, currency);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer", "6000");

			// 根据人民币离岸汇率计算
			BigDecimal exchangeRmbRate = DGSQBaseService.getCurrencyConfig(
					"USD").getExchangeRmbRate();

			amount = amount.multiply(
					BigDecimal.ONE.divide(exchangeRmbRate, 7,
							BigDecimal.ROUND_UP)).setScale(2,
					BigDecimal.ROUND_UP);

			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					currency, currency);

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(500956988L);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_014() {
		Logger.start(false, "直销订单，带有服务费率，未发货，货头申请仅退款，申请金额包含服务费，申请失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithServiceRate(productBean);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(amount);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			Logger.verifyEquals("申请金额大于可退款金额", partAmountRefundCall.getMsg(),
					"验证resultMessage");
			Logger.verifyEquals("3", partAmountRefundCall.getCode(),
					"验证resultCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	@Ignore
	public void Tc_PartAmountRefund_015() {
		Logger.start(false, "直销订单，连锁店模式，未发货，货头申请仅退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) chainStoreGoodsOwnerId, 0, false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeChainStoreOrder(productBean, chainStoreGoodsOwnerId,
							productBean.getCurrency());

			Order order = placeOrderResp.getOrderList().get(0);
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "501476063", "6401");

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(), 6401);

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(chainStoreGoodsOwnerId,
					orderId);

			// 代购神器结算
			ShenqiSettlementService.doSettlement();

			partAmountRefundReq.setUserId(chainStoreGoodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_016() {
		Logger.start(false, "代理订单，已线下付款的订单，货头申请仅退款，退款失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);
			String currency = productBean.getCurrency();
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, BigDecimal.ZERO,
							autotest_goodsOwnerId, currency, true);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			Long userId = placeOrderResp.getUserId();

			// 支付线下支付订单
			SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId(), "wxsqBuyer", "200");
			SqTradingCallService.confirmOfflinePay(orderId,
					autotest_goodsOwnerId);

			Thread.sleep(5000);

			partAmountRefundReq.setUserId(chainStoreGoodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.ONE);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			Logger.verifyEquals("没有操作权限", partAmountRefundCall.getMsg(),
					"验证resultMessage");
			Logger.verifyEquals("3", partAmountRefundCall.getCode(),
					"验证resultCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_017() {
		Logger.start(false, "直销订单，带有各种优惠，货头申请仅退款成功，退最大可退金额");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithAllPromotion(productBean, BigDecimal.TEN,
							autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			BigDecimal maxRefundAmount = RefundService
					.getOrderMaxRefundAmount(order);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(maxRefundAmount);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_018() {
		Logger.start(false, "直销订单，货头申请仅退款成功，退最大可退金额+0.01，退款失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			BigDecimal maxRefundAmount = RefundService.getOrderMaxRefundAmount(
					order).add(new BigDecimal("0.01"));

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(maxRefundAmount);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			Logger.verifyEquals("申请金额大于可退款金额", partAmountRefundCall.getMsg(),
					"验证resultMessage");
			Logger.verifyEquals("3", partAmountRefundCall.getCode(),
					"验证resultCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_019() {
		Logger.start(false, "直销订单，已经部分退款成功的订单，再次仅退款，退最大可退金额，退款成功");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			// 创建一个部分退款成功的退款单
			String refundedBillNo = RefundService
					.createPartProductRefundSuccess(orderId, orderDetailPo,
							autotest_goodsOwnerId, paymentId, userId);

			BigDecimal maxRefundAmount = RefundService
					.getOrderMaxRefundAmount(order);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(maxRefundAmount);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			this.verify(orderId, Arrays.asList(refundedBillNo));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_020() {
		Logger.start(false, "直销订单，已经部分退款、仅退款成功的订单，再次仅退款，退最大可退金额，退款成功");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			// 创建一个部分退款成功的退款单
			String refundedBillNo = RefundService
					.createPartProductRefundSuccess(orderId, orderDetailPo,
							autotest_goodsOwnerId, paymentId, userId);

			// 创建一个仅退款成功的退款单
			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, BigDecimal.TEN);

			BigDecimal maxRefundAmount = RefundService
					.getOrderMaxRefundAmount(order);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(maxRefundAmount);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			this.verify(orderId, Arrays.asList(refundedBillNo));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_021() {
		Logger.start(false, "直销订单多个商品，未发货，退多次，第三次仅退款1分钱，货头申请仅退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProsInOneOrder(productBean);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 创建一个仅退款成功的退款单
			RefundService.createPartAmountRefundSuccess(orderId,
					autotest_goodsOwnerId, paymentId, userId, BigDecimal.TEN);

			// 创建一个仅退款成功的退款单
			RefundService.createPartAmountRefundSuccess(
					orderId,
					autotest_goodsOwnerId,
					paymentId,
					userId,
					RefundService.getOrderMaxRefundAmount(order).subtract(
							new BigDecimal("0.01")));

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(new BigDecimal("0.01"));
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_022() {
		Logger.start(false, "直销订单，货头申请仅退款成功，然后发货，发货成功");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(RefundService
					.getOrderMaxRefundAmount(order));
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			SqTradingCallService.delivery(orderId);

			this.verify(orderId, null, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_023() {
		Logger.start(false, "直销订单，货头申请仅退款，申请金额为0");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.ZERO);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			Logger.verifyEquals(
					"applyAmount:must be greater than or equal to 0.01",
					partAmountRefundCall.getMsg(), "验证resultMessage");
			Logger.verifyEquals("2", partAmountRefundCall.getCode(),
					"验证resultCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_024() {
		Logger.start(false, "直销订单多个商品，未发货，货头申请仅退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProsInOneOrder(productBean);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_025() {
		Logger.start(false,
				"直销订单多个商品，未发货，第一次仅退款10，第二次部分退货退款，第三次仅退款最大可退金额，货头申请仅退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProsInOneOrder(productBean);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			// 创建一个仅退款成功的退款单
			String refundedBillNo1 = RefundService
					.createPartAmountRefundSuccess(orderId,
							autotest_goodsOwnerId, paymentId, userId,
							BigDecimal.TEN);

			// 创建一个部分退货退款成功的退款单
			String refundedBillNo2 = RefundService
					.createPartProductRefundSuccess(orderId, orderDetailPo,
							autotest_goodsOwnerId, paymentId, userId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(RefundService
					.getOrderMaxRefundAmount(order));
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);

			this.verify(orderId,
					Arrays.asList(refundedBillNo1, refundedBillNo2));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_026() {
		Logger.start(false,
				"直销订单多个商品，未发货，第一次部分退货退款，一个商品全退，第2次仅退款最大可退金额，货头申请仅退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProsInOneOrder(productBean);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);
			
			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			OrderDetailPo orderDetailPo = order.getOrderDetailPoList().get(0);

			// 创建一个部分退货退款成功的退款单，其中一个商品全退
			String refundedBillNo = RefundService.createOnePartProductRefundSuccess(orderId,
					orderDetailPo, autotest_goodsOwnerId, paymentId, userId,
					orderDetailPo.getProductCount());

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(RefundService
					.getOrderMaxRefundAmount(order));
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);

			this.verify(orderId,
					Arrays.asList(refundedBillNo));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_027() {
		Logger.start(false, "直销订单，使用积分，已发货，货头申请仅退款");
		try {

			int mchId = DGSQBaseService.createIndependenceMch("USA");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) mchId, 0, false);
			
			GodPointsService.saveCreditPolicyReq((long)mchId);
			Thread.sleep(500);
			
			GodPointsService.sendCredit((long)mchId, 20238699L);
			Thread.sleep(500);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithMultiProsInOneOrderWithIntegral(productBean, 20L);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 发货
			SqTradingCallService.delivery(sellerId, orderId, null, null);

			partAmountRefundReq.setUserId(sellerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(new BigDecimal("100.00"));
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);
			
			RefundService.supplementRefund(orderId);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_028() {
		Logger.start(false, "直销订单，主商品带换购商品，货头申请仅退款成功");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithExchangeBuy(productBean, productBean.getGoodsOwnerId());

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_030() {
		Logger.start(false, "使用中免大会员积分，货头申请仅退款成功");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createMultiCodeProxychain((int) cdfMembergoodsOwnerId, false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithCDFMember(productBean, cdfMembergoodsOwnerId, 10L);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(cdfMembergoodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_031() {
		Logger.start(false, "直销订单，使用代币支付，货头申请仅退款成功");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createIndependenceProxychain((int)virtualCoinGoodsOwnerId, 0, false, false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithUserIdAndGoodsOwnerIdAndCurrency(
							productBean, productBean.getSellerId(),
							productBean.getSellerId(), productBean.getCurrency());

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();
			
			// 充值代币
			SqPretradeService.virtualCoinCharge(virtualCoinGoodsOwnerId,
					productBean.getSellerId(), useAccountAmount);
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			SqTradingCallService
					.payOrderWithVirtualCoin(userId,
							placeOrderResp.getTradingId(), useAccountAmount);
			
			Thread.sleep(5000);
			
			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			partAmountRefundReq.setUserId(virtualCoinGoodsOwnerId);
			partAmountRefundReq.setCsId(virtualCoinGoodsOwnerId);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();
			
			RefundBill refundBill = refundBillWrapper.selectByOrderId(orderId).get(
					0);
			
			RefundService.auditRefund(refundBill.getGoods_owner_id(),
					refundBill.getCurrent_review_mch_id(), 0,
					refundBill.getRefund_no(), true);

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			Thread.sleep(5000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_032() {
		Logger.start(false, "一品多码商品订单，货头申请仅退款成功");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createMultiCodeProxychain((int) multiCDFCodeGoodsOwnerId, false);
			
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeMultiCDFCodeOrder(productBean, multiCDFCodeGoodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(multiCDFCodeGoodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_033() {
		Logger.start(false, "一品多码组合商品订单，使用兑换券，货头申请仅退款成功");
		try {

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeMultiCodeComposeProductOrderWithExchangeCoupon(multiCDFCodeGoodsOwnerId);

//			TradingServiceResp placeOrderResp = SqTradingCallService
//					.placeCDFMemberComposeProductOrder(cdfMembergoodsOwnerId, 0L);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(multiCDFCodeGoodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_034() {
		Logger.start(false, "直销订单，商家通知自提，已发货，自提点未确认收货，货头申请仅退款失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 1);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 发货
			SqTradingCallService.delivery(sellerId, orderId, null, null);

			partAmountRefundReq.setUserId(autotest_goodsOwnerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(4);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			Logger.verifyEquals("商家通知型自提订单, 自提点未收到货时暂时无法发起退款~", partAmountRefundCall.getMsg(),
					"验证resultMessage");
			Logger.verifyEquals("3", partAmountRefundCall.getCode(), "验证resultCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PartAmountRefund_035() {
		Logger.start(false, "中免澳门大会员订单，使用积分抵扣，使用积分，已发货，货头申请仅退款");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) macaoCDFGoodsOwnerId, 0, false, true);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeCDFMacaoOrderIntegral(productBean, 10L);

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();
			
			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getMainOrderId(), "502665591", "7200");

			SqTradingCallService.orderPayCallBack(thirdTradingId, userId,
					amount);

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			PaymentPo payment = orderShardingWapper.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 发货
			SqTradingCallService.delivery(sellerId, orderId, null, null);

			partAmountRefundReq.setUserId(sellerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(new BigDecimal("100.00"));
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());

			Thread.sleep(3000);
			
			RefundService.supplementRefund(orderId);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void PRD_BUG_TEST1() {
		Logger.start(false, "一品多码商品订单，货头申请仅退款成功");
		try {

			TradingServiceResp placeOrderResp = SqTradingCallService
					.PRD_BUG_TEST1();

			Order order = placeOrderResp.getOrderList().get(0);

			long sellerId = order.getSellerId();
			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			SqTradingMqService.updateLiveOrder(false, orderId);

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();

			PaymentPo payment = orderShardingWapper
					.selectPaymentById(paymentId);

			ShenqiSettlementService.onHpSettled(paymentId, payment, null, 1);

			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);

			partAmountRefundReq.setUserId(sellerId);
			partAmountRefundReq.setCsId(1234L);
			partAmountRefundReq.setApplyAmount(BigDecimal.TEN);
			partAmountRefundReq.setOrderId(orderId);
			partAmountRefundReq.setRemark("仅退款");
			partAmountRefundReq.setLiabilityType(1);
			partAmountRefundReq.setRefundExtType(3);
			partAmountRefundReq.setRefundReasonCode(-1);
			partAmountRefundReq.setHasRefundGoods(false);

			partAmountRefundCall.setData(partAmountRefundReq);
			partAmountRefundCall.callService();

			// 退款服务同步到交易
			RefundService.syncNotSyncedRefundBill();

			// 退款提交到网关
			UserRefundFundDetailPo record = new UserRefundFundDetailPo();

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.MINUTE, -6);

			record.setPaymentId(paymentId);
			record.setUpdateTime(calendar.getTime());

			orderShardingWapper.userRefundFundDetailMapper
					.updateByPrimaryKeySelective(record);

			RefundService.supplementRefund(orderId);

			// 支付网关退款回调
			SQPayGateWayCallService.refundNotifyCall("13",
					paymentId.toString(), userId.intValue());
			
			Thread.sleep(3000);

			this.verify(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) {

		// BigDecimal[] weights = new BigDecimal[2];
		//
		// weights[0] = new BigDecimal("2604");
		// weights[1] = new BigDecimal("3960");
		//
		// BigDecimal[] result = Utils.apportionByWeightsAndMaxLimit(new
		// BigDecimal("0.01"), weights, weights,
		// null);
		//
		// for (int i = 0; i < result.length; i++) {
		// System.out.println(result[i]);
		// }

		Ts_PartAmountRefund test = new Ts_PartAmountRefund();

		Long orderId = 14320168699L;

		partAmountRefundReq = new PartAmountRefundReq();

		partAmountRefundReq.setOrderId(orderId);
		partAmountRefundReq.setUserId((long) autotest_goodsOwnerId);
		partAmountRefundReq.setCsId(1234L);
		partAmountRefundReq.setRemark("仅退款");
		partAmountRefundReq.setApplyAmount(new BigDecimal("6721.66"));

		test.verify(orderId,
				Arrays.asList("TH211210000177819", "TH211210000177820"));
	}
}
